package com.qibo.receipt;


import android.Manifest;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qibo.baselib.base.BaseActivity;
import com.qibo.baselib.base.TransparentActivity;
import com.qibo.receipt.fragment.CollectionCodeFragment;
import com.qibo.receipt.fragment.ScanItFragment;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 直接收款
 */
public class PayMainFragmentActivity extends TransparentActivity implements View.OnClickListener,EasyPermissions.PermissionCallbacks {
    private static final int REQUEST_CODE_QRCODE_PERMISSIONS = 1;
    private ScanItFragment scanItFragment;
    private CollectionCodeFragment codeFragment;
    private LinearLayout tab_bottom_ll_scanIt;
    private LinearLayout tab_bottom_ll_collectioncode;
    private TextView textView_titleBar;
    private ImageButton btnBack_pay1;
    private TextView paymain_tv_scanit;
    private TextView paymain_tv_collectioncode;
    @Override
    protected int getContentViewId() {
        return R.layout.receiptmodule_activity_paymain;
    }

    @Override
    protected void initView() {
        tab_bottom_ll_scanIt = findViewById(R.id.tab_bottom_ll_scanIt);
        tab_bottom_ll_collectioncode = findViewById(R.id.tab_bottom_ll_collectioncode);
        paymain_tv_scanit = findViewById(R.id.paymain_tv_scanit);
        paymain_tv_collectioncode = findViewById(R.id.paymain_tv_collectioncode);
        btnBack_pay1 = findViewById(R.id.btnBack_pay1);
        textView_titleBar = findViewById(R.id.textView_titleBar);

        setDefaultFragment();
    }

    @Override
    protected void initAction() {
        tab_bottom_ll_scanIt.setOnClickListener(this);
        tab_bottom_ll_collectioncode.setOnClickListener(this);
        btnBack_pay1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {

    }
    private void setDefaultFragment()
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        scanItFragment = new ScanItFragment();
        paymain_tv_scanit.setTextColor(Color.parseColor(getString(R.color.colorPrimary)));
        paymain_tv_scanit.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_scanit_true, 0, 0, 0);
        paymain_tv_collectioncode.setTextColor(Color.parseColor("#999999"));
        paymain_tv_collectioncode.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_collectioncode, 0, 0, 0);
        transaction.replace(R.id.paymain_content_frame, scanItFragment);
        transaction.commit();
    }

    @SuppressLint("ResourceType")
    @Override
    public void onClick(View view) {
        paymain_tv_scanit.setTextColor(Color.parseColor("#999999"));
        paymain_tv_scanit.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_scanit, 0, 0, 0);
        paymain_tv_collectioncode.setTextColor(Color.parseColor("#999999"));
        paymain_tv_collectioncode.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_collectioncode, 0, 0, 0);
        FragmentManager fm = getSupportFragmentManager();
        // 开启Fragment事务
        FragmentTransaction transaction = fm.beginTransaction();
        int i = view.getId();
        if (i == R.id.tab_bottom_ll_scanIt) {
            paymain_tv_scanit.setTextColor(Color.parseColor(getString(R.color.colorPrimary)));
            paymain_tv_scanit.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_scanit_true, 0, 0, 0);
            if(scanItFragment == null){
                scanItFragment = new ScanItFragment();
            }
            transaction.replace(R.id.paymain_content_frame, scanItFragment);
        } else if (i == R.id.tab_bottom_ll_collectioncode) {
            paymain_tv_collectioncode.setTextColor(Color.parseColor(getString(R.color.colorPrimary)));
            paymain_tv_collectioncode.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_collectioncode_true, 0, 0, 0);
            if(codeFragment == null){
                codeFragment = new CollectionCodeFragment();
            }
            transaction.replace(R.id.paymain_content_frame, codeFragment);
        }
        transaction.commit();
    }
    @Override
    protected void onStart() {
        super.onStart();
        requestCodeQRCodePermissions();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
    }

    @AfterPermissionGranted(REQUEST_CODE_QRCODE_PERMISSIONS)
    private void requestCodeQRCodePermissions() {
        String[] perms = {Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE};
        if (!EasyPermissions.hasPermissions(this, perms)) {
            EasyPermissions.requestPermissions(this, "扫描二维码需要打开相机和散光灯的权限", REQUEST_CODE_QRCODE_PERMISSIONS, perms);
        }
    }
}
